package admincow.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import admincow.dao.UsuarioDAO;
import admincow.vo.UsuarioVO;


public class LoginServlet extends javax.servlet.http.HttpServlet implements
		javax.servlet.Servlet {
	static final long serialVersionUID = 1L;

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public LoginServlet() {
		super();
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String usuario = request.getParameter("usuario");
		String clave = request.getParameter("clave");

		UsuarioDAO dao = new UsuarioDAO();

		try {
			UsuarioVO vo = dao.validar(usuario, clave);
			HttpSession session = request.getSession();
			session.setAttribute("USUARIO_ACTUAL", vo);
			RequestDispatcher rd = request
					.getRequestDispatcher("/principal.jsp");
			rd.forward(request, response);
		} catch (SQLException e) {
			System.out.println("Error");
			RequestDispatcher rd = request.getRequestDispatcher("/bad_user.jsp");
			rd.forward(request, response);
		}

	}
}